package com.itheima.hmshopping.dubbo.apiImpl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.hmshopping.dubbo.api.SmsHomeNewProductServiceApi;
import com.itheima.hmshopping.dubbo.mapper.SmsHomeNewProductMapper;
import com.itheima.hmshopping.entity.SmsHomeNewProduct;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeNewProductServiceApiImpi implements SmsHomeNewProductServiceApi {

    @Autowired
    private SmsHomeNewProductMapper smsHomeNewProductMapper;

    //添加首页新品
    @Override
    public void insert(SmsHomeNewProduct smsHomeNewProduct) {
        smsHomeNewProductMapper.insert(smsHomeNewProduct);
    }

    //批量删除首页新品
    @Override
    public void delete(List<Long> ids) {
        LambdaQueryWrapper<SmsHomeNewProduct> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ObjectUtil.isNotEmpty(ids),SmsHomeNewProduct::getId,ids);
        smsHomeNewProductMapper.delete(lambdaQueryWrapper);
    }

    //分页查询首页新品
    @Override
    public Page<SmsHomeNewProduct> page(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page<SmsHomeNewProduct> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeNewProduct> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //介绍
        if (StrUtil.isNotBlank(productName)){
            lambdaQueryWrapper.like(SmsHomeNewProduct::getProductName,productName);
        }
        //状态
        if (ObjectUtil.isNotEmpty(recommendStatus)){
            lambdaQueryWrapper.eq(SmsHomeNewProduct::getRecommendStatus,recommendStatus);
        }
        lambdaQueryWrapper.orderByAsc(SmsHomeNewProduct::getSort);
        Page<SmsHomeNewProduct> smsHomeNewProductPage = smsHomeNewProductMapper.selectPage(page, lambdaQueryWrapper);
        return smsHomeNewProductPage;
    }

    //批量修改首页新品状态
    @Override
    public void updateList(List<Long> ids, Integer recommendStatus) {
        //查询所有产品
        LambdaQueryWrapper<SmsHomeNewProduct> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ObjectUtil.isNotEmpty(ids),SmsHomeNewProduct::getId,ids);
        List<SmsHomeNewProduct> smsHomeNewProducts = smsHomeNewProductMapper.selectList(lambdaQueryWrapper);
        //批量修改状态
        for (SmsHomeNewProduct smsHomeNewProduct : smsHomeNewProducts) {
            smsHomeNewProduct.setRecommendStatus(recommendStatus);
            smsHomeNewProductMapper.updateById(smsHomeNewProduct);
        }
    }

    //修改首页新品排序
    @Override
    public void updateSort(Long id, Integer sort) {
        SmsHomeNewProduct smsHomeNewProduct = smsHomeNewProductMapper.selectById(id);
        smsHomeNewProduct.setSort(sort);
        smsHomeNewProductMapper.updateById(smsHomeNewProduct);
    }
}
